<template>
  <el-form v-bind="$attrs" @validate="formValidate">
    <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '名称不能为空' }]">
      <el-input
        v-model="formData.name"
        :maxlength="50"
        placeholder="请输入数据标题，不超过50个字符"
      />
    </el-form-item>
    <el-form-item label="资源描述" prop="desc">
      <el-input
        v-model="formData.desc"
        type="textarea"
        :rows="1"
        autosize
        :maxlength="200"
        @keyup.enter.native="specialFormValidate('desc')"
        placeholder="请输入数据的描述信息，不超过200个字符"
      />
    </el-form-item>
    <el-form-item label="样式">
      <div class="row-item">
        <div class="icon-wrap">
          <img class="icon" :src="formData.attr.imageUrl" alt="" @click="picChange" />
        </div>
      </div>
      <div class="row-item">
        <div class="title">高度</div>
        <el-input-number
          v-model="formData.attr.height"
          placeholder=""
          :min="0"
          :controls="false"
          :precision="2"
          @keyup.enter.native="specialFormValidate('height')"
        />
        <div class="unit">米</div>
      </div>
      <div class="row-item">
        <div class="title">速度</div>
        <el-input-number
          v-model="formData.attr.speed"
          placeholder=""
          :min="0"
          :controls="false"
          @keyup.enter.native="specialFormValidate('speed')"
        />
        <div class="unit"></div>
      </div>
    </el-form-item>
    <el-form-item label="颜色">
      <el-color-picker v-model="formData.attr.color" @change="specialFormValidate('color')" />
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { messageUtil } from '@jl/element-plus/utils';
import { retFilePath } from '@/utils';
import { useSceneStore } from '@/store/scene';

defineOptions({
  name: '',
});

interface IProps {
  form: any;
}

const props = withDefaults(defineProps<IProps>(), {
  form: {},
});

const sceneStore = useSceneStore();
const formData = ref<any>(null);
const emits = defineEmits(['formValidate', 'picChange']);

function formValidate(fieldName: string, valid: boolean) {
  updateAttr(fieldName, valid);
}

function specialFormValidate(fieldName: string) {
  let valid = true;
  if (['desc'].includes(fieldName)) {
    if (formData.value[fieldName].length > 200) {
      valid = false;
    }
  }
  if (['height', 'speed'].includes(fieldName)) {
    if (formData.value.attr[fieldName] === '' || isNaN(formData.value.attr[fieldName])) {
      valid = false;
    }
  }
  if (!valid) {
    messageUtil.warning('校验错误');
  }
  updateAttr(fieldName, valid);
}

function updateAttr(fieldName: string, valid: boolean) {
  const { cimInstance } = window;
  sceneStore.editError = !valid;
  if (sceneStore.editError || !sceneStore.selLayer) return;
  if (fieldName === 'height') {
    cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, {
      geometry: { height: sceneStore.selLayer.attr.height },
    });
  } else if (['color', 'speed', 'imageUrl'].includes(fieldName)) {
    cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, {
      material: {
        color: sceneStore.selLayer.attr.color,
        speed: sceneStore.selLayer.attr.speed,
        imageUrl: sceneStore.selLayer.attr.imageUrl,
      },
    });
  }
}

function picChange() {
  emits('picChange');
}

function picChangeCallBack(src: string) {
  if (!sceneStore.selLayer) return;
  sceneStore.selLayer.attr.imageUrl = src;
  specialFormValidate('imageUrl');
}

watchEffect(() => {
  formData.value = props.form;
});

defineExpose({ picChangeCallBack });
</script>

<style lang="less" scoped>
.el-form {
  &-item {
    .row-item {
      width: 100%;
      display: flex;
      margin-bottom: 10px;
      .title {
        flex: 0 0 40px;
      }
      .el-input {
        flex: 1;
      }
      .unit {
        flex: 0 0 24px;
        text-align: center;
      }
      .icon-wrap {
        width: 80px;
        height: 80px;
        .icon {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }
      &:last-child {
        margin-bottom: 0px;
      }
    }
  }
}
</style>
